package com.booby.algorithm.merge;

import com.booby.algorithm.utils.Utils;

/**
 * 功能描述:
 *
 * @author: lizt
 * @date: 2020/8/23 16:54
 **/

public class MaxToMin {

    public static final Integer[] arr = {3,5,7,9,1,2,5,8,10};

    public static void sorted(Integer[] arr, int left, int right){
        if (left == right){
            return;
        }
        int mid = left + (right - left)/2;
        sorted(arr, left, mid);
        sorted(arr, mid + 1, right);
        merge(arr, left, mid + 1, right);
    }

    public static  void merge(Integer[] arr, int lefePtr, int rightPtr, int rightBound){

        Integer[] temp = new Integer[rightBound - lefePtr + 1];
        int left = lefePtr;
        int right = rightPtr;
        int index = 0;
        int mid = rightPtr -1;
        while (left <= mid && right <= rightBound){
            if (arr[left] >= arr[right]){
                temp[index] = arr[left];
                index++;
                left++;
            }else {
                temp[index] = arr[right];
                index++;
                right++;
            }
        }
        while (left <= mid){
            temp[index] = arr[left];
            index++;
            left++;
        }
        while (right <= rightBound){
            temp[index] = arr[right];
            index++;
            right++;
        }
        for (int i = 0; i < temp.length; i++) {
            arr[lefePtr + i] = temp[i];
        }
    }
    public static void main(String[] args) {
        sorted(arr, 0, arr.length-1);
        Utils.print(arr);
    }
}
